package com.web.manager.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.web.manager.pojo.Coursesection;
import com.web.manager.pojo.MajorCourse;
import com.web.manager.pojo.Result;
import com.web.manager.service.CoursesectionService;
import com.web.manager.service.MajorCourseService;
import com.web.manager.pojo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@CrossOrigin  //解决跨域问题
@RestController
@RequestMapping("/majorCourse")
public class MajorCourseController {
    @Autowired
    private MajorCourseService majorCourseService;

    @Resource
    private CoursesectionService coursesectionService;

    @GetMapping("/major/{majorid}")
    public Result findByMajor(@PathVariable Integer majorid){

        List<MajorCourse> deptList = majorCourseService.list(new QueryWrapper<MajorCourse>().eq("majorid",majorid));
        return Result.success(deptList);
    }

    @GetMapping("/{majorid}")
    public Result getById(@PathVariable Integer majorid){

        MajorCourse dept =majorCourseService.getByMajorid(majorid);
        return Result.success(dept);
    }


    //查询专业课程表
    @GetMapping("/courseTable/{majorid}")
    public Result findCourseTable(@PathVariable Integer majorid){

        List<MajorCourse> majorCourses = majorCourseService.list(new QueryWrapper<MajorCourse>().eq("majorid",majorid));


        ArrayList<JSONObject> list = CollUtil.newArrayList();
        JSONObject jsonObject1 = new JSONObject();
        JSONObject jsonObject2 = new JSONObject();
        JSONObject jsonObject3 = new JSONObject();
        JSONObject jsonObject4 = new JSONObject();
        JSONObject jsonObject5 = new JSONObject();

        jsonObject1.set("section", JSONUtil.parseObj("{\"num\":\"第一节\",\"time\":\"08:20-10:00\"}"));
        jsonObject2.set("section",JSONUtil.parseObj("{\"num\":\"第二节\",\"time\":\"10:20-12:00\"}"));
        jsonObject3.set("section",JSONUtil.parseObj("{\"num\":\"第三节\",\"time\":\"14:00-15:40\"}"));
        jsonObject4.set("section",JSONUtil.parseObj("{\"num\":\"第四节\",\"time\":\"16:20-17:40\"}"));
        jsonObject5.set("section",JSONUtil.parseObj("{\"num\":\"第五节\",\"time\":\"18:30-21:30\"}"));
        list.add(jsonObject1);
        list.add(jsonObject2);
        list.add(jsonObject3);
        list.add(jsonObject4);
        list.add(jsonObject5);

        majorCourses.forEach(mc ->{
            Integer courseId = mc.getCoursesectionid();
           Coursesection coursesection =  coursesectionService.getById(courseId);
            String weekday =mc.getWeekday();
            String section = mc.getSection();
            switch (section){
                case "第一节":
                    setJson(weekday,coursesection,jsonObject1);
                    break;
                case "第二节":
                    setJson(weekday,coursesection,jsonObject2);
                    break;
                case "第三节":
                    setJson(weekday,coursesection,jsonObject3);
                    break;
                case "第四节":
                    setJson(weekday,coursesection,jsonObject4);
                    break;
                case "第五节":
                    setJson(weekday,coursesection,jsonObject5);
                    break;
            }
        });


        return Result.success(list);
    }

    private JSONObject setJson(String weekday ,Coursesection coursesection ,JSONObject jsonObject1){
        if("周一".equals(weekday)){
            jsonObject1.set("mon",coursesection);
        } else if("周二".equals(weekday)){
            jsonObject1.set("tue",coursesection);
        } else if("周三".equals(weekday)){
            jsonObject1.set("wes",coursesection);
        } else if("周四".equals(weekday)){
            jsonObject1.set("thu",coursesection);
        } else if("周五".equals(weekday)){
            jsonObject1.set("fri",coursesection);
        } else if("周六".equals(weekday)){
            jsonObject1.set("sat",coursesection);
        } else if("周日".equals(weekday)){
            jsonObject1.set("sun",coursesection);
        }
        return jsonObject1;
    }

    @GetMapping
    public Result list(){

        List<MajorCourse> deptList = majorCourseService.list();
        return Result.success(deptList);
    }

    //新增用户
    @PostMapping("/add")
    public Result add(@RequestBody List<MajorCourse> majorCourses){
        majorCourseService.saveAll(majorCourses);
        return Result.success();
    }

    //修改
    @PutMapping("/update")
    public Result update(@RequestBody MajorCourse type){
       majorCourseService.updateById(type);  //updateUser
        return Result.success();
    }


    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Integer id){

        majorCourseService.removeById(id);  //deleteUser
        return Result.success();
    }

    @DeleteMapping("/delete/batch")
    public Result batchDelete(@RequestBody List<Integer> ids){

        majorCourseService.removeBatchByIds(ids);   //batchDeleteUser
        return Result.success();
    }
    /**
     * 多条件模糊查询用户信息
     * pageNum 当前的页码
     * pageSize 每页查询的个数
     */
    @GetMapping("/selectByPage")
    public Result selectByPage(@RequestParam Integer pageNum,
                               @RequestParam Integer pageSize,
                               @RequestParam String majorid) {

        QueryWrapper<MajorCourse> queryWrapper = new QueryWrapper<>();  //条件构造器
        queryWrapper.like(StrUtil.isNotBlank(majorid),"majorid",majorid);
        //select * from manager_user where username like '%#{username}% and name like '%#{name}%'

        Page<MajorCourse> page = majorCourseService.page(new Page<>(pageNum,pageSize), queryWrapper);        //selectByPage
        return Result.success(page);
    }

}
